home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / dev / src / WBBump_src.lha / WBBump_src / Plugins / blur.wbbplugin.e < prev    next >
Encoding:
Text File  |  1999-06-30  |  6.3 KB  |  375 lines

  1. /* **************** */
  2. /* blur.wbbplugin.e */
  3. /* **************** */
  4.  
  5.  
  6.  
  7. /*
  8.     WBBump - Bumpmapping on the Workbench!
  9.  
  10.     Copyright (C) 1999  Thomas Jensen - dm98411@edb.tietgen.dk
  11.  
  12.     This program is free software; you can redistribute it and/or modify
  13.     it under the terms of the GNU General Public License as published by
  14.     the Free Software Foundation; either version 2 of the License, or
  15.     (at your option) any later version.
  16.  
  17.     This program is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.     GNU General Public License for more details.
  21.  
  22.     You should have received a copy of the GNU General Public License
  23.     along with this program; if not, write to the Free Software Foundation,
  24.     Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. */
  26.  
  27.  
  28.  
  29. /*
  30. */
  31.  
  32. OPT PREPROCESS
  33.  
  34.  
  35.  
  36. LIBRARY 'blur.wbbplugin', 1, 0, 'blur.wbbplugin 1.0 (7/5/99)' IS
  37.     pluginInit(A0), pluginCleanup(),
  38.     pluginInitInstance(A0), pluginFreeInstance(A0),
  39.     pluginGetAttr(D1,D2,A0), pluginSetAttr(D1,D2,D3),
  40.     pluginDoAction(A0,A1,A2,A3)
  41.  
  42.  
  43.  
  44.  
  45.  
  46. /* just to put flags in top of source */
  47.  
  48. #define    ARGTEMPLATE      'PASSES/N'
  49. #define    PLUGINTYPE      PLUGINTYPE_BUMPER
  50. CONST    ISMODIFIER    = TRUE                -> this is a modifying plugin
  51. CONST    ISSTATIC    = TRUE                -> this is a static plugin (see plugin.e)
  52.  
  53.  
  54. /* needed modules */
  55.  
  56.  
  57.  
  58. MODULE    'utility',
  59.         'utility/tagitem'
  60.  
  61.  
  62. MODULE    '*/plugin_const',
  63.         '*/errors',
  64.         '*/argparser',
  65.  
  66.         '*blur_supp'
  67.  
  68.  
  69.  
  70.  
  71. OBJECT handle
  72.     width    :    LONG
  73.     height    :    LONG
  74.     args    :    PTR TO CHAR
  75.     rdargs    :    LONG
  76.     passes    :    LONG
  77.     tempbuf    :    PTR TO CHAR
  78. ENDOBJECT
  79.  
  80.  
  81.  
  82.  
  83. DEF    lasterr=NIL
  84.  
  85.  
  86.  
  87.  
  88. PROC main()
  89.     /* library init code here */
  90.     /* this is executed in Forbid() so be carefull */
  91.     /* better use pluginInit() for most things */
  92.  
  93.     /* we need utility.library for GetTagData() */
  94.     IF (utilitybase := OpenLibrary('utility.library', 37)) = NIL THEN RETURN FALSE
  95.  
  96.  
  97.  
  98. ENDPROC
  99.  
  100.  
  101.  
  102. PROC close()
  103.     /* free stuff allocated in main() here */
  104.  
  105.     IF utilitybase THEN CloseLibrary(utilitybase)
  106.  
  107. ENDPROC
  108.  
  109.  
  110.  
  111.  
  112. /* init the "class" */
  113.  
  114. PROC pluginInit(tags:PTR TO tagitem)
  115.     /* do initializations that can't be done in main() here */
  116.     /* read files, open libs, etc. */
  117.  
  118. ENDPROC TRUE
  119.  
  120.  
  121.  
  122.  
  123. /* free the "class" */
  124.  
  125. PROC pluginCleanup()
  126.     /* cleanup things done in pluginInit() */
  127.  
  128. ENDPROC
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. /* init an instance */
  139.  
  140. PROC pluginInitInstance(tags:PTR TO tagitem) HANDLE
  141.     DEF    h=NIL:PTR TO handle,
  142.         args=NIL:PTR TO LONG
  143.  
  144.     /* this function is allways called by WBBump */
  145.     /* use it to allocate buffers, etc */
  146.  
  147.     /* return a handle to an "instance" */
  148.  
  149.  
  150.     /* alloc handle */
  151.     NEW h
  152.  
  153.  
  154.  
  155.     /* set global vars */
  156.  
  157.     IF tags
  158.         h.width        := GetTagData(PLUGINTAG_WIDTH, -1, tags)
  159.         h.height    := GetTagData(PLUGINTAG_HEIGHT, -1, tags)
  160.         h.args        := GetTagData(PLUGINTAG_ARGS, NIL, tags)
  161.         args        := [0]
  162.         h.rdargs    := parseargs(ARGTEMPLATE, args, h.args)
  163.         IF h.rdargs = NIL THEN eThrow(-1, 'Bad arguments, template is %s', [ARGTEMPLATE])
  164.         h.passes    := IF args[0] THEN Long(args[0]) ELSE 1
  165.         IF h.passes > 1 THEN h.tempbuf := NewR(Mul(h.height, h.width))
  166.         IF h.passes < 1 THEN eThrow(-1, 'Wrong number of passes: %ld', [h.passes])
  167.     ELSE
  168.         RETURN NIL
  169.     ENDIF
  170.  
  171.  
  172.  
  173.     /* check size */
  174.     IF    (h.width <= 0) OR (h.height <= 0)
  175.         Throw(-1, 'No or wrong size given')
  176.     ENDIF
  177.  
  178.     /* return NIL if there was an error */
  179.  
  180. EXCEPT DO
  181.     IF exception
  182.         IF h THEN pluginFreeInstance(h)
  183.         lasterr := exceptioninfo
  184.         RETURN NIL
  185.     ENDIF
  186. ENDPROC h
  187.  
  188.  
  189.  
  190.  
  191.  
  192. /* free an instance */ 
  193.  
  194. PROC pluginFreeInstance(h:PTR TO handle)
  195.  
  196.     /* cleanup stuff allocated in pluginInitInstance() */
  197.  
  198.     IF h.rdargs THEN freeargs(h.rdargs)
  199.  
  200.     IF h.tempbuf THEN Dispose(h.tempbuf)
  201.  
  202. ENDPROC
  203.  
  204.  
  205.  
  206. /* get an attribute from the instance, or globally if handle = 0 */
  207.  
  208. PROC pluginGetAttr(h:PTR TO handle, attr, valueptr:PTR TO LONG)
  209.  
  210.     /* this is where WBBump gets information from the plugin */
  211.     /* look in plugin.e to see what is required */
  212.  
  213.     IF h
  214.         SELECT attr
  215.  
  216.         CASE PLUGINTAG_WIDTH
  217.             valueptr[0] := h.width    -> return the width
  218.             RETURN TRUE
  219.  
  220.         CASE PLUGINTAG_HEIGHT
  221.             valueptr[0] := h.height    -> return the height
  222.             RETURN TRUE
  223.  
  224.         CASE PLUGINTAG_NEEDUPDATE
  225.             valueptr[0] := FALSE
  226.             RETURN TRUE
  227.  
  228.         ENDSELECT
  229.     ENDIF
  230.  
  231.     SELECT attr
  232.  
  233.     CASE PLUGINTAG_WIDTH
  234.         valueptr[0] := -1
  235.         RETURN TRUE
  236.  
  237.     CASE PLUGINTAG_HEIGHT
  238.         valueptr[0] := -1
  239.         RETURN TRUE
  240.  
  241.     CASE PLUGINTAG_ISMODIFIER
  242.         valueptr[0] := ISMODIFIER    -> see top of file
  243.         RETURN TRUE
  244.  
  245.     CASE PLUGINTAG_COMMANDNAME
  246.         valueptr[0] := 'PLUGIN_BLUR'    -> tooltypes command
  247.         RETURN TRUE
  248.  
  249.     CASE PLUGINTAG_ISSTATIC
  250.         valueptr[0] := ISSTATIC        -> see top of file
  251.         RETURN TRUE
  252.  
  253.     CASE PLUGINTAG_TYPE
  254.         valueptr[0] := PLUGINTYPE
  255.         RETURN TRUE
  256.  
  257.     CASE PLUGINTAG_NAME
  258.         valueptr[0] := 'blur.wbbplugin'
  259.         RETURN TRUE
  260.  
  261.     CASE PLUGINTAG_COPYRIGHT
  262.         valueptr[0] := '©1999 Thomas Jensen - dm98411@edb.tietgen.dk'
  263.         RETURN TRUE
  264.  
  265.     CASE PLUGINTAG_AUTHOR
  266.         valueptr[0] := 'Thomas Jensen - dm98411@edb.tietgen.dk'
  267.         RETURN TRUE
  268.  
  269.     CASE PLUGINTAG_DESC
  270.         valueptr[0] := 'Blurs the input image'
  271.         RETURN TRUE
  272.  
  273.     CASE PLUGINTAG_LASTERROR
  274.         valueptr[0] := lasterr
  275.         RETURN TRUE
  276.  
  277.     DEFAULT
  278.         lasterr := 'Unknown tag'
  279.         RETURN FALSE            -> return false if the tag is unknown
  280.  
  281.     ENDSELECT
  282.  
  283. ENDPROC FALSE
  284.  
  285.  
  286.  
  287. PROC pluginSetAttr(h:PTR TO handle, attr, value)
  288.  
  289.     /* later this may be used to set plugin attributes */
  290.     /* if there's no attributes to set, return FALSE */
  291.  
  292. ENDPROC FALSE
  293.  
  294.  
  295.  
  296.  
  297. PROC pluginDoAction(h:PTR TO handle, inbuf:PTR TO CHAR, outbuf:PTR TO CHAR, tags:PTR TO tagitem) HANDLE
  298.     DEF    i,
  299.         b1, b2, bx
  300.  
  301.     /* this is the "action" part of the plugin */
  302.     /* it is called each time the plugin's services are needed */
  303.     /* if the plugin says TRUE to ISMODIFIER then inbuf MAY contain an image */
  304.     /* othervise it a NIL pointer */
  305.     /* tags is currently not used */
  306.  
  307.     IF inbuf <> NIL
  308.  
  309.         IF And(h.passes, 1) = 0
  310.             b1 := h.tempbuf
  311.             b2 := outbuf
  312.         ELSE
  313.             b1 := outbuf
  314.             b2 := h.tempbuf
  315.         ENDIF
  316.         blur_buffer(inbuf, b1, h.width, h.height)
  317.         FOR i := 0 TO h.passes-2
  318.             blur_buffer(b1, b2, h.width, h.height)
  319.             bx := b1
  320.             b1 := b2
  321.             b2 := bx
  322.         ENDFOR
  323.  
  324.     ELSE
  325.         FOR i := 0 TO Mul(h.width, h.height)-1 DO outbuf[i] := 0
  326.     ENDIF
  327.  
  328.  
  329. EXCEPT DO
  330.     /* return FALSE if something went wrong */
  331.     IF exception
  332.         lasterr := exceptioninfo
  333.         RETURN FALSE
  334.     ENDIF
  335. ENDPROC TRUE
  336.  
  337.  
  338. /*
  339. theori:
  340.  
  341.  
  342. 1 pass
  343.     i t o
  344.     -------
  345. 1   i   o
  346.  
  347.  
  348.  
  349. 2 pass
  350.     i t o
  351.     -------
  352. 1   i o
  353. 2      i o
  354.  
  355.  
  356. 3 pass
  357.     i t o
  358.     -------
  359. 1   i   o
  360. 2      o i
  361. 3      i o
  362.  
  363.  
  364.  
  365. 4 pass
  366.     i t o
  367.     -------
  368. 1   i o
  369. 2      i o
  370. 3      o i
  371. 4      i o
  372.  
  373. */
  374.  
  375.